package pm.gui;

import pm.devices.drives.Drive;
import pm.oo.NativeOOViewComponent;
import pm.gui.frames.AdminFrame;
import pm.gui.frames.PMFrame;
import pm.gui.components.basepanels.*;
import pm.gui.components.basepanels.DocumentViewPanel;
import pm.config.Configuration;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.io.File;
import java.math.BigDecimal;
import pm.gui.frames.PrintDialog;

/**
 * Common implementation of <code>IGuiModule</code> that contains
 * platform independent code.
 *
 * @author Anton Bernatski
 */
public class GuiModuleImpl implements GuiModule {

	public enum Panel {
		Splash,
		Main,
		FileChoose,
		DocumentView,
		Copy,
		Scan,
        Money
    }

	protected GuiModuleListener guiModuleListener;
	private Configuration configuration;

	//FramesCreator
	protected PMFrame pmFrame;
	protected AdminFrame adminFrame;
	//Base Panels:
	protected SplashPanel splashPanel;
	protected MainPanel mainPanel;
	protected DocumentViewPanel documentViewPanel;
	protected FileChoosePanel fileChoosePanel;
        //Dialogs
        protected PrintDialog printDialog;

	public GuiModuleImpl(final GuiModuleListener guiModuleListener, Configuration configuration) {
		this.guiModuleListener = guiModuleListener;
		this.configuration = configuration;
		init();
	}

	private void init() {
		GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
		final GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
		for (GraphicsDevice device : devices) {
			device.getConfigurations();
			System.out.println("device.getIDstring() = " + device.getIDstring());
			System.out.println("device.getType() = " + device.getType());
//			if ("\\Display0".equals(device.getIDstring()));
			
//			throw new RuntimeException("debug");
		}

		this.pmFrame = new PMFrame(devices[0].getDefaultConfiguration());

		//AdminFrame init
		this.adminFrame = new AdminFrame();
		this.pmFrame.addKeyListener(new KeyAdapter() {
                        @Override
			public void keyTyped(final KeyEvent e) {
				if (configuration.getAdminKey().charAt(0) == e.getKeyChar()) {
					adminFrame.setVisible(true);
				}
			}
		});

		//Splash init
		this.splashPanel = new SplashPanel();

		//Main init
		this.mainPanel = new MainPanel();
		this.mainPanel.printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPanelPrintButton();
			}
		});
		this.mainPanel.copyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPanelCopyButton();
			}
		});
		this.mainPanel.scanButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPanelScanButton();
			}
		});

		//DocumentView init
		this.documentViewPanel = new DocumentViewPanel();
		this.documentViewPanel.printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				documentViewPanelPrintButton();
			}
		});
		this.documentViewPanel.nextButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				documentViewPanelNextButton();
			}
		});
		this.documentViewPanel.previousButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				documentViewPanelPreviousButton();
			}
		});
		this.documentViewPanel.backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				backButton(Panel.DocumentView);
			}
		});

		//FileChoose init
		this.fileChoosePanel = new FileChoosePanel();
		this.fileChoosePanel.driveComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				driveSelected();
			}
		});
		this.fileChoosePanel.backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				backButton(Panel.FileChoose);
			}
		});
		this.fileChoosePanel.openButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				openFile();
			}
		});
                
                this.printDialog = new PrintDialog();
                this.printDialog.printButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        print();
                    }
                });
    }

	public void showSplashScreen() {
		pmFrame.setBasePanel(splashPanel);
	}

	public void showMainScreen() {
		pmFrame.setBasePanel(mainPanel);
	}

	public void showPrintScreen() {
        pmFrame.setBasePanel(documentViewPanel);
    }

	public void showFileChooseDialog() {
		fileChoosePanel.driveComboBox.setSelectedIndex(-1);
		fileChoosePanel.textRadioButton.setSelected(true);
		pmFrame.setBasePanel(fileChoosePanel);
	}

	public void showCopyScreen() {
	}

	public void showScanScreen() {

	}

	public void showPrintDialog(final boolean customPrintingAvailable) {
            this.printDialog.setVisible(true);
	}

	public NativeOOViewComponent getNativeOOViewComponent(){
		return documentViewPanel.getNativeOOViewComponent();
	}

	public void updateMoneyState(final BigDecimal money) {
	}

	public void setDrives(Drive[] drives) {
		this.fileChoosePanel.setDevices(drives);
	}

	//BackButtons from all panels proccessing
	private void backButton(Panel panel) {
		switch (panel) {
			case FileChoose:
				showMainScreen();
                break;
            case DocumentView:
                guiModuleListener.closeCurrentDocument();
                showMainScreen();
                break;
        }
	}

	//Main events proccessing
	private void mainPanelScanButton() {
		//todo show ScanScreen
	}

	private void mainPanelPrintButton() {
		showFileChooseDialog();
	}

	private void mainPanelCopyButton() {
        //todo show CopyScreen
    }

	//DocumentViewPanel events proccessing
	private void documentViewPanelPrintButton() {
        guiModuleListener.printDocument();
    }

	private void documentViewPanelNextButton() {
        guiModuleListener.nextPage();
    }

	private void documentViewPanelPreviousButton() {
        guiModuleListener.previousPage();
    }

	//FileChoosePanel events proccessing
	private void driveSelected() {
		if (fileChoosePanel.driveComboBox.getSelectedItem() == null) {
			return;
		}
		Drive drive = (Drive) fileChoosePanel.driveComboBox.getSelectedItem();
		if (new File(drive.getId() + ":\\").listFiles() == null) {
			this.fileChoosePanel.fileChooseScrollPane.setVisible(false);
            //todo
            this.fileChoosePanel.driveNotSelectedLabel.setVisible(true);
			return;
		}

		this.fileChoosePanel.driveNotSelectedLabel.setVisible(false);
		this.fileChoosePanel.fileChooseScrollPane.setVisible(true);
		this.fileChoosePanel.changeDir(drive.getId() + ":\\");
	}

	private void openFile() {
		Object value = this.fileChoosePanel.filesList.getSelectedValue();
		if (value instanceof File) {
			File file = (File) value;
				guiModuleListener.openFile(file);
		}
	}
        
        private void print() {
            //this.printDialog.currentPage
        }
}
